<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :answerPage="true" @answerInsert="answerInsert" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
        :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="状态">
            <el-select v-model="form.status" placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.type" placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in optionsType" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="关数">
            <el-input v-model="form.commonId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="问题">
            <el-input v-model="form.question" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="答案A">
            <el-input v-model="answer.answerA" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="答案B">
            <el-input v-model="answer.answerB" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="答案C" v-if="form.type !== '3'">
            <el-input v-model="answer.answerC" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="答案D" v-if="form.type !== '3'">
            <el-input v-model="answer.answerD" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="正确答案">
            <div v-if="form.correct">
              <el-input  v-model="form.correct.split(',').join('')" style="width: 370px;" />
            </div>
          </el-form-item>
          <el-form-item label="问题顺序">
            <el-input v-model="form.recommend" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cancel">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="confirm">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
        @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{scope.row.status=='1' ? "正常" : scope.row.status=='0' ? '异常' : '错误'}}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{scope.row.type=='1' ? "单选" : scope.row.type=='2' ? '多选' : scope.row.type=='3' ? '判断':'错误'}}
          </template>
        </el-table-column>
        <el-table-column prop="commonId" label="关数" />
        <el-table-column prop="question" label="问题" />
        <el-table-column prop="answer" label="答案" />
        <el-table-column prop="correct" label="正确答案" />
        <el-table-column prop="recommend" label="问题顺序" />
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column prop="updatedAt" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','dccQuestion:edit','dccQuestion:del'])" label="操作" width="150px"
          align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :answerPage="true" @answerSplit="answerSplit" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
  import crudDccQuestion from '@/api/dccQuestion'
  import CRUD, {
    presenter,
    header,
    form,
    crud
  } from '@crud/crud'
  import rrOperation from '@crud/RR.operation'
  import crudOperation from '@crud/CRUD.operation'
  import udOperation from '@crud/UD.operation'
  import pagination from '@crud/Pagination'

  const defaultForm = {
    id: null,
    status: null,
    type: null,
    commonId: null,
    question: null,
    answer: null,
    correct: null,
    operator: null,
    recommend: null,
    createdAt: null,
    updatedAt: null
  }
  export default {
    name: 'DccQuestion',
    components: {
      pagination,
      crudOperation,
      rrOperation,
      udOperation
    },
    mixins: [presenter(), header(), form(defaultForm), crud()],
    cruds() {
      return CRUD({
        title: '毒查查问题',
        url: 'api/dccQuestion',
        idField: 'id',
        sort: 'id,desc',
        crudMethod: {
          ...crudDccQuestion
        }
      })
    },
    data() {
      return {
        answer: {},
        options: [{
          value: 1,
          label: '正常'
        }, {
          value: 0,
          label: '异常'
        }],
        optionsType: [{
          value: '1',
          label: '单选'
        }, {
          value: '2',
          label: '多选'
        }, {
          value: '3',
          label: '判断'
        }],
        permission: {
          add: ['admin', 'dccQuestion:add'],
          edit: ['admin', 'dccQuestion:edit'],
          del: ['admin', 'dccQuestion:del']
        },
        rules: {}
      }
    },
    methods: {
      // 钩子：在获取表格数据之前执行，false 则代表不获取数据
      [CRUD.HOOK.beforeRefresh]() {
        return true
      },
      answerInsert () {
        this.answer = {}
      },
      answerSplit (item) {
        let arr = item.answer.split(',')
        console.log(arr)
        let arr1 = arr.map(item =>{
          return item.split("|")[1]
        })
        console.log(arr1)
        const [ answerA, answerB, answerC, answerD ] = arr1
        this.answer = {answerA, answerB, answerC, answerD}
      },
      cancel () {
        this.answer = {}
        this.crud.cancelCU()
      },
      confirm () {
        const { answerA, answerB, answerC, answerD } = this.answer
        if (this.form.type === '3') {
          this.form.answer = [ "A|"+answerA, "B|"+answerB ].join(',')
        } else {
          this.form.answer = [ "A|"+answerA, "B|"+answerB, "C|"+answerC, "D|"+answerD ].join(',')
        }
        const correct = this.form.correct;
        this.form.correct = Array.from(correct.toUpperCase()).join(',')
        this.crud.submitCU()
      }
    }
  }
</script>

<style scoped>

</style>
